home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / OWLSRC.PAK / UIBORDER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.9 KB  |  316 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #if !defined(OWL_UIHELPER_H)
  10. # include <owl/uihelper.h>
  11. #endif
  12. #if !defined(OWL_GDIOBJEC_H)
  13. # include <owl/gdiobjec.h>
  14. #endif
  15.  
  16. OWL_DIAGINFO;
  17.  
  18. //
  19. // Construct a UIBorder object given a frame and a hilevel style type.
  20. // Calculates Edge and modifier flags internally as needed.
  21. //
  22. TUIBorder::TUIBorder(const TRect& frame, TUIBorder::TStyle style, uint flags)
  23. :
  24.   Frame(frame)
  25. {
  26.   PRECONDITION(style >= 0 && style <= WellSet);
  27.  
  28.   static struct {
  29.     uint Edge;
  30.     uint Flags;
  31.   }
  32.   styleMap[] = {
  33.     {0,                         0},    // None
  34.     {0,                         0},    // Plain
  35.     {RaisedOuter,               0},    // Raised
  36.     {SunkenOuter,               0},    // Recessed
  37.     {RaisedOuter | SunkenInner, 0},    // Embossed
  38.     {SunkenOuter | RaisedInner, 0},    // Grooved
  39.     {RaisedOuter | RaisedInner, Soft}, // ButtonUp
  40.     {SunkenOuter | SunkenInner, Soft}, // ButtonDn
  41.     {RaisedOuter | RaisedInner, 0},    // WndRaised
  42.     {SunkenOuter | SunkenInner, 0},    // WndRecessed
  43.     {SunkenOuter | RaisedInner, 0},    // WellSet ???
  44.   };
  45.   Edge = styleMap[style].Edge;
  46.   Flags = styleMap[style].Flags | flags;
  47.   if (!(Flags & Rect))
  48.     Flags |= Rect;
  49. }
  50.  
  51. //
  52. // Construct a UIBorder object given a frame and edge and modifier flags
  53. //
  54. TUIBorder::TUIBorder(const TRect& frame, TEdge edge, uint flags)
  55. :
  56.   Frame(frame),
  57.   Edge(edge),
  58.   Flags(flags)
  59. {
  60.   if (!(Flags & Rect))
  61.     Flags |= Rect;
  62. }
  63.  
  64. //
  65. // Move the frame rect by (dx,dy)
  66. //
  67. void
  68. TUIBorder::Move(int dx, int dy)
  69. {
  70.   Frame.Offset(dx, dy);
  71. }
  72.  
  73. //
  74. // Move the frame rect to (x,y)
  75. //
  76. void
  77. TUIBorder::MoveTo(int x, int y)
  78. {
  79.   Frame.Offset(x-Frame.left, y-Frame.top);
  80. }
  81.  
  82. //
  83. // Resize the frame rect to (w,h)
  84. //
  85. void
  86. TUIBorder::Size(int w, int h)
  87. {
  88.   Frame.right = Frame.left + w;
  89.   Frame.top = Frame.top + h;
  90. }
  91.  
  92. //
  93. // Calculate the outside frame rectangle
  94. //
  95. TRect
  96. TUIBorder::GetBoundingRect() const
  97. {
  98. //  return Style == WellSet ?  Frame.InflatedBy(1,1) : Frame;
  99.   return Frame;
  100. }
  101.  
  102. //
  103. // Calculate the rectangle within the border
  104. //
  105. TRect
  106. TUIBorder::GetClientRect() const
  107. {
  108.   int count = ((Edge & EdgeOuter) ? 1 : 0) + ((Edge & EdgeInner) ? 1 : 0);
  109.   return Frame.InflatedBy(-count,-count);
  110. }
  111.  
  112. //
  113. // Paint this UIBorder object onto a given dc
  114. //
  115. void
  116. TUIBorder::Paint(TDC& dc) const
  117. {
  118.   DrawEdge(dc, Frame, Edge, Flags);
  119.  
  120. //  case WellSet:
  121. //      PaintWT(dc, Frame.InflatedBy(1,1));
  122. //      Paint3H(dc, Frame);
  123. //      PaintWT(dc, Frame.InflatedBy(-1,-1));
  124. }
  125.  
  126. //
  127. // Static function that performs the actual drawing of edges for a UIBorder,
  128. // or an external client. Uses the system ::DrawEdge if available.
  129. //
  130. bool
  131. TUIBorder::DrawEdge(TDC& dc, const TRect& frame, uint edge, uint flags)
  132. {
  133. #if defined(BI_PLAT_WIN32)
  134.   static int hasDrawEdge = true;
  135.  
  136.   // Try once to see if the API call is available. If not, do ourselves.
  137.   //
  138.   if (hasDrawEdge) {
  139.     if (::DrawEdge(dc, (LPRECT)&frame, edge, flags))
  140.       return true;
  141.     if (::GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
  142.       hasDrawEdge = false;
  143.     else
  144.       return false;
  145.   }
  146. #endif
  147.  
  148.   // ::DrawEdge is not available, do the drawing ourselves
  149.   //
  150.   TRect f(frame);  // working frame rectangle
  151.  
  152.   // If mono is set, draw a thin, flat, black (windowFrame) frame
  153.   //
  154.   if (flags & Mono) {
  155.     if (edge & EdgeOuter) {
  156.       PaintFrame(dc, f, flags, TColor::SysWindowFrame, TColor::SysWindowFrame);
  157.       f.Inflate(-1,-1);
  158.     }
  159.     if (flags & Fill) { 
  160.       TBrush brsh(TColor::SysWindow);
  161.       dc.SelectObject(brsh);
  162.       dc.PatBlt(f);
  163.       dc.RestoreBrush();
  164.     }
  165.     return true;
  166.   }
  167.  
  168.   // If flat is set, draw a thin, flat, shadow frame
  169.   //
  170.   if (flags & Flat) {
  171.     if (edge & EdgeOuter) {
  172.       PaintFrame(dc, f, flags, TColor::Sys3dShadow, TColor::Sys3dShadow);
  173.       f.Inflate(-1,-1);
  174.     }
  175.     if (flags & Fill) { 
  176.       TBrush brsh(TColor::Sys3dFace);
  177.       dc.SelectObject(brsh);
  178.       dc.PatBlt(f);
  179.       dc.RestoreBrush();
  180.     }
  181.     return true;
  182.   }
  183.  
  184.   // Draw outer edge if indicated, adjusting rect afterwards
  185.   //
  186.   if (edge & EdgeOuter) {
  187.     static TColor tlColors[] = {
  188.       TColor::Sys3dLight,       // EdgeRaised
  189.       TColor::Sys3dHilight,     // EdgeRaised + Soft
  190.       TColor::Sys3dShadow,      // EdgeSunken
  191.       TColor::Sys3dDkShadow,    // EdgeSunken + Soft
  192.     };
  193.     static TColor brColors[] = {
  194.       TColor::Sys3dDkShadow,    // EdgeRaised
  195.       TColor::Sys3dDkShadow,    // EdgeRaised + Soft
  196.       TColor::Sys3dHilight,     // EdgeSunken
  197.       TColor::Sys3dHilight,     // EdgeSunken + Soft
  198.     };
  199.     int ci = ((edge & SunkenOuter) ? 2 : 0) | ((flags & Soft) ? 1 : 0);
  200.     PaintFrame(dc, f, flags, tlColors[ci], brColors[ci]);
  201.     f.Inflate(-1,-1);
  202.   }
  203.  
  204.   // Draw inner edge if indicated, adjusting rect afterwards
  205.   //
  206.   if (edge & EdgeInner) {
  207.     static TColor tlColors[] = {
  208.       TColor::Sys3dHilight,     // EdgeRaised
  209.       TColor::Sys3dLight,       // EdgeRaised + Soft
  210.       TColor::Sys3dDkShadow,    // EdgeSunken
  211.       TColor::Sys3dShadow,      // EdgeSunken + Soft
  212.     };
  213.     static TColor brColors[] = {
  214.       TColor::Sys3dShadow,      // EdgeRaised
  215.       TColor::Sys3dShadow,      // EdgeRaised + Soft
  216.       TColor::Sys3dLight,       // EdgeSunken
  217.       TColor::Sys3dLight,       // EdgeSunken + Soft
  218.     };
  219.     int ci = ((edge & SunkenOuter) ? 2 : 0) | ((flags & Soft) ? 1 : 0);
  220.     PaintFrame(dc, f, flags, tlColors[ci], brColors[ci]);
  221.     f.Inflate(-1,-1);
  222.   }
  223.  
  224.   // Fill interior if indicated
  225.   //
  226.   if (flags & Fill) {
  227.     TBrush brsh(TColor::Sys3dFace);
  228.     dc.SelectObject(brsh);
  229.     dc.PatBlt(f);
  230.     dc.RestoreBrush();
  231.   }
  232.  
  233. //  if (flags & Adjust)
  234. //    frame = f;
  235.  
  236.   return true;
  237. }
  238.  
  239. //
  240. // Paint a 2-color single pixel thick frame, bevel corners get br color
  241. //
  242. void
  243. TUIBorder::PaintFrame(TDC& dc, const TRect& fr, uint flags, const TColor& tlColor, const TColor& brColor)
  244. {
  245.   if (flags & (Top | Left)) {
  246.     TBrush brsh(tlColor);
  247.     dc.SelectObject(brsh);
  248.     if (flags & Top)
  249.       dc.PatBlt(fr.left, fr.top, fr.Width()-1, 1);
  250.     if (flags & Left)
  251.       dc.PatBlt(fr.left, fr.top+1, 1, fr.Height()-2);
  252.     dc.RestoreBrush();
  253.   }
  254.  
  255.   if (flags & (Bottom | Right)) {
  256.     TBrush brsh(brColor);
  257.     dc.SelectObject(brsh);
  258.     if (flags & Bottom)
  259.       dc.PatBlt(fr.left, fr.bottom-1, fr.Width(), 1);
  260.     if (flags & Right)
  261.       dc.PatBlt(fr.right-1, fr.top, 1, fr.Height()-1);
  262.     dc.RestoreBrush();
  263.   }
  264. }
  265.  
  266. //
  267. // Paint a 2-color single pixel thick frame, bevel corners get their own color
  268. //
  269. void
  270. TUIBorder::PaintFrameC(TDC& dc, const TRect& fr, uint flags, const TColor& tlColor, const TColor& brColor, const TColor& bcColor)
  271. {
  272.   if (flags & (Top | Left)) {
  273.     TBrush brsh(tlColor);
  274.     dc.SelectObject(brsh);
  275.     if (flags & Top) {
  276.       dc.PatBlt(fr.left, fr.top, fr.Width()-2, 1);
  277.       dc.SetPixel(fr.right-1, fr.top, bcColor);
  278.     }
  279.     if (flags & Left)
  280.       dc.PatBlt(fr.left, fr.top+1, 1, fr.Height()-2);
  281.     dc.RestoreBrush();
  282.   }
  283.  
  284.   if (flags & (Bottom | Right)) {
  285.     TBrush brsh(brColor);
  286.     dc.SelectObject(brsh);
  287.     if (flags & Bottom) {
  288.       dc.SetPixel(fr.left, fr.bottom-1, bcColor);
  289.       dc.PatBlt(fr.left+1, fr.bottom-1, fr.Width(), 1);
  290.     }
  291.     if (flags & Right)
  292.       dc.PatBlt(fr.right-1, fr.top, 1, fr.Height()-1);
  293.     dc.RestoreBrush();    
  294.   }
  295. }
  296.  
  297. #if 0
  298. //
  299. //
  300. //
  301. void
  302. TUIBorder::PaintWT(TDC& dc, const TRect& frame)
  303. {
  304.   PaintFrame(dc, frame, TColor::SysWindowText, TColor::SysWindowText);
  305. }
  306.  
  307. //
  308. //
  309. //
  310. void
  311. TUIBorder::Paint3H(TDC& dc, const TRect& frame)
  312. {
  313.   PaintFrame(dc, frame, TColor::Sys3dHilight, TColor::Sys3dHilight);
  314. }
  315. #endif
  316.